package cn.com.easy.permission.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.easy.permission.dao.UserLoginRecordDao;
import cn.com.easy.permission.model.UserLoginRecordModel;
import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.dto.MessageDTO;
import cn.com.easy.permission.web.dto.PageRequestParamDTO;
import cn.com.easy.utils.PageUtils;
import cn.com.easy.utils.ResponseOutputUtils;

/**
 * 登录记录
 * 
 * @author nibili 2017年12月13日
 * 
 */
@Controller
@RequestMapping("/manage/login/record")
public class SystemLoginRecordController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SystemLoginRecordController.class);

	/** 用户登录记录dao */
	@Autowired
	private UserLoginRecordDao userLoginRecordDao;

	/**
	 * 登录记录首页
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 * @author nibili 2017年12月13日
	 */
	@UrlReserveAnotion("登录记录")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String loginRecord(HttpServletRequest request) throws Exception {
		return "/system/system_login_record/system_login_record";
	}

	/**
	 * 获取所有登录记录
	 * 
	 * @param request
	 * @param response
	 * @param pageRequestParamDTO
	 * @param searchText
	 * @author nibili 2017年12月13日
	 */
	@UrlReserveAnotion("获取所有登录记录")
	@RequestMapping("/get/all")
	public void getAllRecord(HttpServletRequest request, HttpServletResponse response, PageRequestParamDTO pageRequestParamDTO, String searchText) {
		try {
			Page<UserLoginRecordModel> springDataPage;
			if (StringUtils.isNotBlank(searchText) == true) {
				springDataPage = userLoginRecordDao.findByLoginNameLikeOrRealNameLike(searchText, pageRequestParamDTO.buildSpringDataPageRequest());
			} else {
				springDataPage = userLoginRecordDao.findAll(pageRequestParamDTO.buildSpringDataPageRequest());
			}
			cn.com.easy.utils.Page<UserLoginRecordModel> page = PageUtils.getPage(springDataPage);
			// page.setTotalItems(1000);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			logger.error("获取所有登录异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取所有登录记录异常.ex:" + ex.getLocalizedMessage()));
		}
	}

}
